using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Windows.Forms;
using System.Drawing;
using Ent.Web.Helper;
using System.Drawing.Imaging;
using System.IO;
using Ent.Voice.Nomination;

public partial class GenerateThumb : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            int id = MathFunction.ConvertToInteger(Request.QueryString["id"], -1);
            if (id != -1)
            {
                Entry en = Entry.GetEntry(id);
                string imagePath = "/" + ConfigurationManager.AppSettings["WebImage"] + id.ToString() + ".png";
                Bitmap oBitmap;
                if (!System.IO.File.Exists(HttpRuntime.AppDomainAppPath + imagePath))
                {
                    int w = MathFunction.ConvertToInteger(Request.QueryString["w"], -1);
                    int h = MathFunction.ConvertToInteger(Request.QueryString["h"], -1);
                    oBitmap = WebsiteThumbnailImageGenerator.GetWebSiteThumbnail(en.Upload.Location, 800, 600, w, h);
                    Common.Log.Error("bitmap created");
                    using (FileStream fs = new FileStream(HttpRuntime.AppDomainAppPath + imagePath, FileMode.Create, FileAccess.Write))
                    {
                        oBitmap.Save(fs, System.Drawing.Imaging.ImageFormat.Jpeg);
                        Common.Log.Error("image created");
                    }
                }
                else
                    oBitmap = new Bitmap(HttpRuntime.AppDomainAppPath + imagePath);
                Response.ContentType = "image/png";
                MemoryStream ms = new MemoryStream();
                oBitmap.Save(ms, ImageFormat.Png);
                ms.WriteTo(Response.OutputStream);
                Response.Flush();
                oBitmap.Dispose();
                Response.End();
            }
        }
        catch (Exception exc)
        {
            Common.Log.Error(exc.Message);
            Common.ErrorPage(exc);
        }
    }
}
